﻿using AutoMapper;
using FakeXiecheng.API.Dtos;
using FakeXiecheng.API.ResourceParameters;
using FakeXiecheng.API.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FakeXiecheng.API.Models;

namespace myNetCore.Controllers
{
    /// <summary>
    /// 核心业务:旅游路线API Controller
    /// </summary>
    [Route("api/[controller]")] // api/touristroute
    [ApiController]
    public class TouristRoutesController : ControllerBase
    {
        private ITouristRouteRepository _touristRouteRepository;
        private readonly IMapper _mapper;

        public TouristRoutesController(ITouristRouteRepository touristRouteRepository)
        {
            _touristRouteRepository = touristRouteRepository;
        }

        /// <summary>
        /// 测试用于返回所有符合条件的旅游线路
        /// api/touristRoutes?keyword=XXX&rating=largerThan2
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HttpHead]
        public IActionResult GetTouristRoutes(
            [FromQuery] TouristRouteResourceParamaters paramaters
        //[FromQuery] string keyword,
        //string rating // 小于lessThan, 大于largerThan, 等于equalTo lessThan3, largerThan2, equalTo5 
        )// FromQuery vs FromBody
        {
            //var touristRoutesFromRepo = _touristRouteRepository.GetTouristRoutes(keyword,operatorType,ratingValue);
            var touristRoutesFromRepo = _touristRouteRepository.GetTouristRoutes(paramaters.Keyword, paramaters.RatingOperator, paramaters.RatingValue);
            if (touristRoutesFromRepo == null || touristRoutesFromRepo.Count() <= 0)
            {
                return NotFound("没有旅游路线");
            }
            var touristRoutesDto = _mapper.Map<IEnumerable<TouristRouteDto>>(touristRoutesFromRepo);
            return Ok(touristRoutesDto);
        }

        // api/touristes/{touristRoutedId}
        /// <summary>
        /// 根据touristRoutedId返回单条旅游线路,设置函数名GetTouristRouteById,可以用于当创建路线成功后返回调用此函数,关联CreatedAtRoute路径参数
        /// </summary>
        /// <param name="touristRoutedId"></param>
        /// <returns></returns>
        [HttpGet("{touristRouteId}", Name = "GetTouristRouteById")]
        public IActionResult GetTouristRouteById(Guid touristRoutedId)
        {
            var touristRouteFromRepo = _touristRouteRepository.GetTouristRoute(touristRoutedId);
            if (touristRouteFromRepo == null)
            {
                return NotFound($"旅游线路{touristRoutedId}找不到");
            } 
            return Ok();
        }

        /// <summary>
        /// 创建一个新的POST actin函数
        /// </summary>
        /// <param name="touristRouteForCreationDto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult CreateTouristRoute([FromBody] TouristRouteForCreationDto touristRouteForCreationDto)
        {
            var touristRouteModel = _mapper.Map<TouristRoute>(touristRouteForCreationDto);//关联TouristRouteProfile.cs, 需要映射到类型是TouristRoute,数据touristRouteForCreationDto作为参数
            _touristRouteRepository.AddTouristRoute(touristRouteModel);
            _touristRouteRepository.Save();
            var touristRouteToReture = _mapper.Map<TouristRouteDto>(touristRouteModel);
            //POST请求成功返回201,
            return CreatedAtRoute(
                "GetTouristRouteById",//与返回的head头部Location
                new { touristRouteId = touristRouteToReture.Id },//API路径参数
                touristRouteToReture //参数三POST请求成功后需要输出的参数
            );
        }
    }
}
